package com.pengtu.service.receive;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.receive.EntrustDao;
import com.pengtu.dao.receive.TrialsiteDao;
import com.pengtu.dao.receive.Trialwt01Dao;
import com.pengtu.dao.receive.Trialwt02Dao;
import com.pengtu.dao.receive.Trialwt03Dao;
import com.pengtu.dao.receive.Trialwt04Dao;
import com.pengtu.dao.receive.Trialwt05Dao;
import com.pengtu.dao.receive.Trialwt06Dao;
import com.pengtu.dao.receive.Trialwt07Dao;
import com.pengtu.dao.receive.Trialwt08Dao;
import com.pengtu.dao.receive.Trialwt09Dao;
import com.pengtu.entity.receive.Entrust;
import com.pengtu.entity.receive.Trialsite;
import com.pengtu.entity.receive.Trialwt01;
import com.pengtu.entity.receive.Trialwt02;
import com.pengtu.entity.receive.Trialwt03;
import com.pengtu.entity.receive.Trialwt04;
import com.pengtu.entity.receive.Trialwt05;
import com.pengtu.entity.receive.Trialwt06;
import com.pengtu.entity.receive.Trialwt07;
import com.pengtu.entity.receive.Trialwt08;
import com.pengtu.entity.receive.Trialwt09;
import com.pengtu.entity.trial.Order;
import com.pengtu.service.BaseManager;
import com.pengtu.service.NumberManager;
import com.pengtu.service.trial.TrialManager;

@Component
@Transactional
public class TrialCommonManager extends BaseManager{

	private EntrustDao entrustDao;
	private TrialsiteDao trialsiteDao;
	private Trialwt01Dao trialwt01Dao;
	private Trialwt02Dao trialwt02Dao;
	private Trialwt03Dao trialwt03Dao;
	private Trialwt04Dao trialwt04Dao;
	private Trialwt05Dao trialwt05Dao;
	private Trialwt06Dao trialwt06Dao;
	private Trialwt07Dao trialwt07Dao;
	private Trialwt08Dao trialwt08Dao;
	private Trialwt09Dao trialwt09Dao;
	private TrialManager trialManager;
	private NumberManager numberManager;
	//操作entrust
	
	public void saveEntrust(Entrust entrust) {
		entrustDao.save(entrust);
	}
	
	public void deleteEntrust(String id) {
		entrustDao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Entrust FindByProEntrust(String propertyName , String value) {
		return entrustDao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Entrust> findByEntrustList(QueryFilter queryFilter) {
		return entrustDao.find(queryFilter);
	}
	
	//操作trialsite
	
	public void saveTrialsite(Trialsite trialsite) {
		trialsiteDao.save(trialsite);
	}
	
	public void deleteTrialsite(String id) {
		trialsiteDao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Trialsite FindByProTrialsite(String propertyName , String value) {
		return trialsiteDao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialsite> findByTrialsiteList(QueryFilter queryFilter) {
		return trialsiteDao.find(queryFilter);
	}
	//操作trialwt01
	
	public void saveTrialwt01(Trialwt01 trialwt01) {
		trialwt01Dao.save(trialwt01);
	}
	
	public void deleteTrialwt01(String id) {
		trialwt01Dao.delete(id);
	}
	public void deleteOrderTrialwt01sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt01>  oldTrialwt01s = order.getTrialwt01s();
			Iterator<Trialwt01> lo =  oldTrialwt01s.iterator();
			while(lo.hasNext()){
				Trialwt01  trialwt01 = lo.next();
				trialwt01Dao.deleteTrialwt01Entity(trialwt01.getId());
			}
		}
	}

	public void saveTrialwt01All(Set<Trialwt01> pp) {
		Iterator<Trialwt01> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt01  trialwt01 = ls.next();
			trialwt01.setCode(numberManager.getRandomNum());
			trialwt01Dao.save(trialwt01);
		}
	}
	
	
	@Transactional(readOnly=true)
	public Trialwt01 FindByProTrialwt01(String propertyName , String value) {
		return trialwt01Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt01> findByTrialwt01List(QueryFilter queryFilter) {
		return trialwt01Dao.find(queryFilter);
	}
	//操作trialwt02
	
	public void saveTrialwt02(Trialwt02 trialwt02) {
		trialwt02Dao.save(trialwt02);
	}
	
	public void deleteTrialwt02(String id) {
		trialwt02Dao.delete(id);
	}
	
	public void deleteOrderTrialwt02sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt02>  oldTrialwt02s = order.getTrialwt02s();
			Iterator<Trialwt02> lo =  oldTrialwt02s.iterator();
			while(lo.hasNext()){
				Trialwt02  trialwt02 = lo.next();
				trialwt02Dao.deleteTrialwt02Entity(trialwt02.getId());
			}
		}
	}

	public void saveTrialwt02All(Set<Trialwt02> pp) {
		Iterator<Trialwt02> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt02  trialwt02 = ls.next();
			trialwt02.setCode(numberManager.getRandomNum());
				trialwt02Dao.save(trialwt02);
		}
	}
	
	
	@Transactional(readOnly=true)
	public Trialwt02 FindByProTrialwt02(String propertyName , String value) {
		return trialwt02Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt02> findByTrialwt02List(QueryFilter queryFilter) {
		return trialwt02Dao.find(queryFilter);
	}
	//操作trialwt03
	
	public void saveTrialwt03(Trialwt03 trialwt03) {
		trialwt03Dao.save(trialwt03);
	}
	
	public void deleteTrialwt03(String id) {
		trialwt03Dao.delete(id);
	}
	
	public void deleteOrderTrialwt03sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt03>  oldTrialwt03s = order.getTrialwt03s();
			Iterator<Trialwt03> lo =  oldTrialwt03s.iterator();
			while(lo.hasNext()){
				Trialwt03  trialwt03 = lo.next();
				trialwt03Dao.deleteTrialwt03Entity(trialwt03.getId());
			}
		}
	}

	public void saveTrialwt03All(Set<Trialwt03> pp) {
		Iterator<Trialwt03> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt03  trialwt03 = ls.next();
			trialwt03.setCode(numberManager.getRandomNum());
				trialwt03Dao.save(trialwt03);
		}
	}
	
	
	@Transactional(readOnly=true)
	public Trialwt03 FindByProTrialwt03(String propertyName , String value) {
		return trialwt03Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt03> findByTrialwt03List(QueryFilter queryFilter) {
		return trialwt03Dao.find(queryFilter);
	}
	//操作trialwt04
	
	public void saveTrialwt04(Trialwt04 trialwt04) {
		trialwt04Dao.save(trialwt04);
	}
	
	public void deleteTrialwt04(String id) {
		trialwt04Dao.delete(id);
	}
	
	public void deleteOrderTrialwt04sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt04>  oldTrialwt04s = order.getTrialwt04s();
			Iterator<Trialwt04> lo =  oldTrialwt04s.iterator();
			while(lo.hasNext()){
				Trialwt04  trialwt04 = lo.next();
				trialwt04Dao.deleteTrialwt04Entity(trialwt04.getId());
			}
		}
	}

	public void saveTrialwt04All(Set<Trialwt04> pp) {
		Iterator<Trialwt04> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt04  trialwt04 = ls.next();
			trialwt04.setCode(numberManager.getRandomNum());
				trialwt04Dao.save(trialwt04);
		}
	}
	
	@Transactional(readOnly=true)
	public Trialwt04 FindByProTrialwt04(String propertyName , String value) {
		return trialwt04Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt04> findByTrialwt04List(QueryFilter queryFilter) {
		return trialwt04Dao.find(queryFilter);
	}
	//操作trialwt05
	public void saveTrialwt05(Trialwt05 trialwt05) {
		trialwt05Dao.save(trialwt05);
	}
	
	public void deleteTrialwt05(String id) {
		trialwt05Dao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Trialwt05 FindByProTrialwt05(String propertyName , String value) {
		return trialwt05Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt05> findByTrialwt05List(QueryFilter queryFilter) {
		return trialwt05Dao.find(queryFilter);
	}
	//操作trialwt06
	public void saveTrialwt06(Trialwt06 trialwt06) {
		trialwt06Dao.save(trialwt06);
	}
	
	public void deleteTrialwt06(String id) {
		trialwt06Dao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Trialwt06 FindByProTrialwt06(String propertyName , String value) {
		return trialwt06Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt06> findByTrialwt06List(QueryFilter queryFilter) {
		return trialwt06Dao.find(queryFilter);
	}
	//操作trialwt07
	
	public void saveTrialwt07(Trialwt07 trialwt07) {
		trialwt07Dao.save(trialwt07);
	}
	
	public void deleteTrialwt07(String id) {
		trialwt07Dao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Trialwt07 FindByProTrialwt07(String propertyName , String value) {
		return trialwt07Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt07> findByTrialwt07List(QueryFilter queryFilter) {
		return trialwt07Dao.find(queryFilter);
	}
	//操作trialwt08
	
	public void saveTrialwt08(Trialwt08 trialwt08) {
		trialwt08Dao.save(trialwt08);
	}
	
	public void deleteTrialwt08(String id) {
		trialwt08Dao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Trialwt08 FindByProTrialwt08(String propertyName , String value) {
		return trialwt08Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt08> findByTrialwt08List(QueryFilter queryFilter) {
		return trialwt08Dao.find(queryFilter);
	}
	//操作trialwt09
	
	public void saveTrialwt09(Trialwt09 trialwt09) {
		trialwt09Dao.save(trialwt09);
	}
	
	public void deleteTrialwt09(String id) {
		trialwt09Dao.delete(id);
	}
	
	@Transactional(readOnly=true)
	public Trialwt09 FindByProTrialwt09(String propertyName , String value) {
		return trialwt09Dao.findUniqueBy(propertyName, value);
	}
	
	@Transactional(readOnly=true)
	public List<Trialwt09> findByTrialwt09List(QueryFilter queryFilter) {
		return trialwt09Dao.find(queryFilter);
	}
	

	@Autowired
	public void setEntrustDao(EntrustDao entrustDao) {
		this.entrustDao = entrustDao;
	}

	@Autowired
	public void setTrialsiteDao(TrialsiteDao trialsiteDao) {
		this.trialsiteDao = trialsiteDao;
	}

	@Autowired
	public void setTrialwt01Dao(Trialwt01Dao trialwt01Dao) {
		this.trialwt01Dao = trialwt01Dao;
	}

	@Autowired
	public void setTrialwt02Dao(Trialwt02Dao trialwt02Dao) {
		this.trialwt02Dao = trialwt02Dao;
	}

	@Autowired
	public void setTrialwt03Dao(Trialwt03Dao trialwt03Dao) {
		this.trialwt03Dao = trialwt03Dao;
	}

	@Autowired
	public void setTrialwt04Dao(Trialwt04Dao trialwt04Dao) {
		this.trialwt04Dao = trialwt04Dao;
	}

	@Autowired
	public void setTrialwt05Dao(Trialwt05Dao trialwt05Dao) {
		this.trialwt05Dao = trialwt05Dao;
	}

	@Autowired
	public void setTrialwt06Dao(Trialwt06Dao trialwt06Dao) {
		this.trialwt06Dao = trialwt06Dao;
	}

	@Autowired
	public void setTrialwt07Dao(Trialwt07Dao trialwt07Dao) {
		this.trialwt07Dao = trialwt07Dao;
	}

	@Autowired
	public void setTrialwt08Dao(Trialwt08Dao trialwt08Dao) {
		this.trialwt08Dao = trialwt08Dao;
	}

	@Autowired
	public void setTrialwt09Dao(Trialwt09Dao trialwt09Dao) {
		this.trialwt09Dao = trialwt09Dao;
	}
	
	@Autowired
	public void setTrialManager(TrialManager trialManager) {
		this.trialManager = trialManager;
	}

	@Transactional(readOnly=true)
	public Page<Entrust> findByEntrustPage(Page<Entrust> page, QueryFilter queryFilter) {
		return entrustDao.findPage(page, queryFilter);
	}
	

	public void deleteOrderTrialwt05sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt05>  oldTrialwt05s = order.getTrialwt05s();
			Iterator<Trialwt05> lo =  oldTrialwt05s.iterator();
			while(lo.hasNext()){
				Trialwt05  trialwt05 = lo.next();
				trialwt05Dao.deleteTrialwt05Entity(trialwt05.getId());
			}
		}
		
	}

	public void saveTrialwt05All(Set<Trialwt05> pp) {
		Iterator<Trialwt05> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt05  trialwt05 = ls.next();
			trialwt05.setCode(numberManager.getRandomNum());
			//trialwt05.setCode();
				trialwt05Dao.save(trialwt05);
		}
	}
	
	public void deleteOrderTrialwt06sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt06>  oldTrialwt06s = order.getTrialwt06s();
			Iterator<Trialwt06> lo =  oldTrialwt06s.iterator();
			while(lo.hasNext()){
				Trialwt06  trialwt06 = lo.next();
				trialwt06Dao.deleteTrialwt06Entity(trialwt06.getId());
			}
		}
		
	}

	public void saveTrialwt06All(Set<Trialwt06> pp) {
		Iterator<Trialwt06> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt06  trialwt06 = ls.next();
		//	trialwt06.setCode(numberManager.getRandomNum());
			trialwt06.setCode(numberManager.getLargestByType("WT06"));
				trialwt06Dao.save(trialwt06);
		}
	}
	
	public void deleteOrderTrialwt07sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt07>  oldTrialwt07s = order.getTrialwt07s();
			Iterator<Trialwt07> lo =  oldTrialwt07s.iterator();
			while(lo.hasNext()){
				Trialwt07  trialwt07 = lo.next();
				trialwt07Dao.deleteTrialwt07Entity(trialwt07.getId());
			}
		}
		
	}

	public void saveTrialwt07All(Set<Trialwt07> pp) {
		Iterator<Trialwt07> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt07  trialwt07 = ls.next();
			trialwt07.setCode(numberManager.getRandomNum());
				trialwt07Dao.save(trialwt07);
		}
	}
	
	public void deleteOrderTrialwt08sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt08>  oldTrialwt08s = order.getTrialwt08s();
			Iterator<Trialwt08> lo =  oldTrialwt08s.iterator();
			while(lo.hasNext()){
				Trialwt08  trialwt08 = lo.next();
				trialwt08Dao.deleteTrialwt08Entity(trialwt08.getId());
			}
		}
		
	}

	public void saveTrialwt08All(Set<Trialwt08> pp) {
		Iterator<Trialwt08> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt08  trialwt08 = ls.next();
			trialwt08.setCode(numberManager.getRandomNum());
				trialwt08Dao.save(trialwt08);
		}
	}
	
	public void deleteOrderTrialwt09sByEntrust(Entrust entrust) {
		Set<Order> ll =  entrust.getOrders();
		Iterator<Order> ls =  ll.iterator();
		while(ls.hasNext()){
			Order  order = ls.next();
			trialManager.deleteOrderEntity(order.getId());
		}
		if(ls.hasNext()){
			Order  order = ls.next();
			Set<Trialwt09>  oldTrialwt09s = order.getTrialwt09s();
			Iterator<Trialwt09> lo =  oldTrialwt09s.iterator();
			while(lo.hasNext()){
				Trialwt09  trialwt09 = lo.next();
				trialwt09Dao.deleteTrialwt09Entity(trialwt09.getId());
			}
		}
		
	}

	public void saveTrialwt09All(Set<Trialwt09> pp) {
		Iterator<Trialwt09> ls =  pp.iterator();
		while(ls.hasNext()){
			Trialwt09  trialwt09 = ls.next();
			trialwt09.setCode(numberManager.getRandomNum());
				trialwt09Dao.save(trialwt09);
		}
	}

	@Autowired
	public void setNumberManager(NumberManager numberManager) {
		this.numberManager = numberManager;
	}
	
}
